﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Najak
{
	public partial class Scene
	{
		public class Manager
		{
			static public string SceneFolder = "../../../ContentHigh/Scenes/";

			static public readonly Manager Instance = new Manager();

			public List<string> SceneNames = new List<string>();
			public Scene CurrentScene;

			private Manager()
			{
				string[] files = System.IO.Directory.GetFiles(SceneFolder);
				foreach (string f in files)
				{
					string fn = System.IO.Path.GetFileNameWithoutExtension(f);
					SceneNames.Add(fn);
				}
				SceneNames.Sort();
			}

			public Scene LoadNewScene()
			{
				string Scene = Editor.GetString.GetCommand();
				System.IO.File.Copy("./Content/Defaults/DefaultScene.xml", SceneFolder + Scene + ".xml");
				SceneNames.Add(Scene);
				SceneNames.Sort();

				int index = Manager.Instance.GetIndexOf(Scene);
				Portal.PortalTarget pd = new Portal.PortalTarget(index.ToString() + ",0,W");

				return LoadSceneByName(Scene, pd);
			}

			private void OnLeavingScene(string nextSceneName)
			{
				if (CurrentScene != null)
				{
					if (!CurrentScene.DoNotSaveOnLeaving)
					{
						CurrentScene.SaveIfNeeded();
					}
					CurrentScene.OnLeaving();
				}
			}

			public Scene LoadSceneByName(string Scene, Portal.PortalTarget sendingPortal)
			{
				OnLeavingScene(Scene);
				CurrentScene = new Scene(App.Instance.AppHost.Services, Scene, sendingPortal);
				return CurrentScene;
			}

			public string GetNextSceneName(string Scene)
			{
				int index = GetIndexOf(Scene);

				index++;
				if (index >= SceneNames.Count)
				{
					index = SceneNames.Count - 1;
				}
				return SceneNames[index];
			}

			public string GetPreviousSceneName(string Scene)
			{
				int index = GetIndexOf(Scene);

				index--;
				if (index < 0)
				{
					index = 0;
				}
				return SceneNames[index];
			}

			public int GetIndexOf(string Scene)
			{
				return SceneNames.IndexOf(Scene);
			}

			public void DeleteCurrentScene()
			{
				if (CurrentScene != null && App.IsEditorMode)
				{
					bool wantToDelete = Editor.Yes_No.GetAnswer("Do you want to delete this scene?");

					if (wantToDelete != true)
						return;

					string deleteSceneName = CurrentScene.Name;
					string goToScene = GetPreviousSceneName(deleteSceneName);

					if (goToScene == deleteSceneName)
					{
						goToScene = GetNextSceneName(deleteSceneName);

						if (goToScene == deleteSceneName)
						{
							System.Windows.Forms.MessageBox.Show("You must have at least one scene");
							return;
						}
					}

					Portal.PortalTarget pd = new Portal.PortalTarget(goToScene + ",0,W");

					LoadSceneByName(goToScene, pd);

					System.IO.File.Delete(SceneFolder + deleteSceneName + ".txt");
					SceneNames.Remove(deleteSceneName);
				}
			}

			public void RenameCurrentScene()
			{
				string newSceneName = Editor.GetString.GetCommand();

				if (newSceneName == "" || newSceneName == CurrentScene.Name)
					return;

				if (SceneNames.Contains(newSceneName))
				{
					System.Windows.Forms.MessageBox.Show("That already exists!");
					return;
				}

				string currentScenename = CurrentScene.Name;
				SceneNames.Remove(currentScenename);

				SceneNames.Add(newSceneName);
				SceneNames.Sort();
				CurrentScene.Name = newSceneName;

				System.IO.File.Move(SceneFolder + currentScenename + ".txt", SceneFolder + newSceneName + ".txt.");
			}
		}
	}

}